package com.tougutips.like.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tougutips.common.like.vo.LikeMeVo;
import com.tougutips.common.like.vo.LikeVO;
import com.tougutips.common.like.vo.UserLikesVo;
import com.tougutips.like.mapper.CookBookMapper;
import com.tougutips.like.mapper.UserMapper;
import com.tougutips.like.service.CookBookDao;
import com.tougutips.like.service.UserDao;
import com.tougutips.like.entity.Cookbook;
import com.tougutips.like.entity.LikeAll;
import com.tougutips.like.entity.User;
import com.tougutips.like.enums.LikeEnums;
import com.tougutips.like.enums.LikeSetNameEnum;
import com.tougutips.like.mapper.LikeMapper;
import com.tougutips.like.service.LikeService;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
public class LikeServiceImpl extends ServiceImpl<LikeMapper, LikeAll> implements LikeService {
    @Resource
    private LikeMapper likeMapper;
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private RabbitTemplate rabbitTemplate;
    @Autowired(required = true)
    private CookBookMapper cookBookDao;
    @Autowired(required = true)
    private UserMapper userDao;

    @Override
    public void like(LikeVO userLike) {
        String setName = LikeSetNameEnum.getTypeName(userLike.getLikeType());
        //判断赞是否已经存在
        Boolean aBoolean = redisTemplate.opsForHash().hasKey(setName + ":" + userLike.getLikeId(), userLike.getUserId() + "");
        if (!aBoolean) {
            rabbitTemplate.convertAndSend(LikeEnums.EXCHANGE_NAME.getValue(), LikeEnums.LIKE_QUEUE_ROUTING_KEY.getValue(), userLike);
        }
    }

    @Override
    public void noLike(LikeVO userLike) {
        String setName = LikeSetNameEnum.getTypeName(userLike.getLikeType());
        if (redisTemplate.opsForHash().hasKey(setName + ":" + userLike.getLikeId(), userLike.getUserId() + "")){
            redisTemplate.opsForHash().delete(setName + ":" + userLike.getLikeId(),userLike.getUserId() + "");
            QueryWrapper<LikeAll> wrapper = new QueryWrapper<>();
            wrapper.eq("user_id",userLike.getUserId());
            wrapper.eq("like_id",userLike.getLikeId());
            wrapper.eq("like_type",userLike.getLikeType());
            wrapper.eq("like_user_id",userLike.getLikeUserId());
            likeMapper.delete(wrapper);
        }
    }

    @Override
    public List<UserLikesVo> getLikes(long userid) {
        QueryWrapper<LikeAll> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id", userid);
        wrapper.orderByDesc("likeTime");
        List<LikeAll> likeAlls = baseMapper.selectList(wrapper);
        List<UserLikesVo> userLikesVos = new ArrayList<>();
        for (LikeAll likeAll : likeAlls) {
            UserLikesVo property = new UserLikesVo();
            BeanUtils.copyProperties(likeAll, property);
            Cookbook cookbook = cookBookDao.selectById(property.getLikeId());
            if (property.getLikeType() == LikeSetNameEnum.COOKBOOK_LIKE_SET.getType() && cookbook != null) {
                property.setCover(cookbook.getCover());
                property.setName(cookbook.getCookbookName());
            }
            userLikesVos.add(property);
        }
        return userLikesVos;
    }

    @Override
    public int getProLike(long id, int type) {
        Long size = 0L;
        switch (type) {
            //查询视频赞的数量
            case 0:
                break;
            //查询菜谱赞的数量
            case 1:
                if (Boolean.TRUE.equals(redisTemplate.opsForSet().isMember(LikeSetNameEnum.COOKBOOK_LIKE_SET.getTypeName(), id + ""))) {
                    size = redisTemplate.opsForHash().size(LikeSetNameEnum.COOKBOOK_LIKE_SET.getTypeName() + ":" + id);
                    break;
                } else {
                    break;
                }
                //查询分享赞的数量
            case 2:
                break;
            //查询评论赞的数量
            case 3:
                break;
        }
        return Integer.parseInt(size.toString());
    }

    @Override
    public List<LikeMeVo> getLikeMe(long userid) {
        QueryWrapper<LikeAll> wrapper = new QueryWrapper<>();
        wrapper.eq("like_user_id", userid);
        wrapper.orderByDesc("likeTime");
        List<LikeAll> likeAlls = baseMapper.selectList(wrapper);
        ArrayList<LikeMeVo> likeMeVos = new ArrayList<>();
        for (LikeAll likeAll : likeAlls) {
            LikeMeVo likeMeVo = new LikeMeVo();
            BeanUtils.copyProperties(likeAll, likeMeVo);
            User user = userDao.selectById(likeAll.getUserId());
            if (user != null) {
                likeMeVo.setNickName(user.getNickname());
                likeMeVo.setAvatar(user.getAvatar());
                if (likeAll.getLikeType() == LikeSetNameEnum.COOKBOOK_LIKE_SET.getType()) {
                    Cookbook cookbook = cookBookDao.selectById(likeAll.getLikeId());
                    likeMeVo.setCover(cookbook.getCover());
                }
                likeMeVos.add(likeMeVo);
            }
        }
        return likeMeVos;
    }


}
